<?php
namespace Controller;

class RegisterController {
	/**
	 * @return String $xhtml
	 */
	public function DoControll($database) {
		$xhtml = "";
		$messageid = 0;
		$message = "";

		$rv = new \View\RegisterView();
		$rh = new \Model\RegisterHandler($database);

		//Kollar om användaren försökt registrera sig och returnerar ett meddelande.

		if ($rv -> TriedToRegister()) {
			$messageid = $rh -> RegisterUser($rv -> GetUsername(), $rv -> GetPassword(), $rv -> GetPassword2());

			if ($messageid == \Model\RegisterHandler::PASSWORD_MISSMATCH) {
				$message .= $rv -> TryRegMess(\View\RegisterView::PASSWORD_ON_BOTH_WRONG);
			}
			if ($messageid == \Model\RegisterHandler::FIELDS_EMPTY) {
				$message .= $rv -> TryRegMess(\View\RegisterView::FIELDS_EMPTY);
			}
			if ($messageid == \Model\RegisterHandler::USERNAME_IN_USE) {
				$message .= $rv -> TryRegMess(\View\RegisterView::USERNAME_IN_USE);
			}
			if ($messageid == \Model\RegisterHandler::PASSWORD_WRONG_FORMAT) {
				$message .= $rv -> TryRegMess(\View\RegisterView::PASSWORD_WRONG_FORMAT);
			}
			if ($messageid == \Model\RegisterHandler::REG_SUCCESS) {
				$message .= $rv -> TryRegMess(\View\RegisterView::REG_SUCCESS);

			}
		}

		$xhtml .= $message;
		$xhtml .= $rv -> DoRegisterForm();

		return $xhtml;
	}

}
